package com.kmproject.haifu.mvp.presenter;

import android.app.Application;
import android.support.annotation.NonNull;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.MapSelectAddressContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.beans.GooglePlaceInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.GooglePlaceInfoTextSearchBean;

import javax.inject.Inject;

import io.reactivex.annotations.Nullable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/03/2019 00:44
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class MapSelectAddressPresenter extends BasePresenter<MapSelectAddressContract.Model, MapSelectAddressContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MapSelectAddressPresenter(MapSelectAddressContract.Model model, MapSelectAddressContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 周边搜索
     *
     * @param lat
     * @param lng
     */
    public void nearbySearchRequest(@NonNull String lat, @NonNull String lng) {
        mModel.nearbySearchRequest("https://maps.googleapis.com/maps/api/place/nearbysearch/json", Api.google_map_key, lat, lng, null, 500).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<GooglePlaceInfoBean>(mErrorHandler) {
                    @Override
                    public void onNext(GooglePlaceInfoBean s) {
                        mRootView.nearbySearchRequestSuccess(s);
                    }
                });
    }

    /**
     * 关键字模糊搜索
     *
     * @param keyword
     * @param lat
     * @param lng
     * @param region  cn：中国地区；hk：香港；mo：澳门；tw：台湾；ph：菲律宾。{可以使用 region 参数将 Google Maps Geocoding API 设置为返回偏向于特定地区的结果。该参数带有指定地区偏向的 ccTLD（国家代码顶级域）自变量。大多数 ccTLD 代码与 ISO 3166-1 代码相同，但有一些明显的例外。例如，英国的国家代码顶级域名为“uk”(.co.uk)，而其 ISO 3166-1 代码却是“gb”（专指“大不列颠及北爱尔兰联合王国”这一实体）。
     *                <p>
     *                地理编码结果可以偏向于正式推出了主要 Google Maps 应用的每个区域。请注意，偏向只是倾向于使用某个特定区域的结果；如果在此区域以外存在更相关的结果，也可能会将这些结果包括在内。}
     */
    public void keywordSearchRequest(@NonNull String keyword, @Nullable String lat, @Nullable String lng, @NonNull String region) {
        mModel.keywordSearchRequest("https://maps.googleapis.com/maps/api/place/textsearch/json", Api.google_map_key, keyword, lat, lng, null, 1000, region).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<GooglePlaceInfoTextSearchBean>(mErrorHandler) {
                    @Override
                    public void onNext(GooglePlaceInfoTextSearchBean s) {
                        mRootView.showMessage(s.getStatus());
                        mRootView.keywordSearchRequestSuccess(s);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
